/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.bophanDAO;
import dao.hanghoaDAO;
import dao.nhanvienDAO;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.table.DefaultTableModel;
import org.hibernate.HibernateException;
import pojo.*;

/**
 *
 * @author HoaDinhGiang
 */
public class QuanLyMatKhauNhanVienInternalFrame1 extends javax.swing.JInternalFrame {

    /**
     * Creates new form QuanLyNhanVienMatKhauInternalFrame
     */
    private List<Nhanvien> dsNhanVien;
    private DefaultComboBoxModel comboBoxModelLoaiNhanVien;
    public int maPhieuXuat;
    public QuanLyMatKhauNhanVienInternalFrame1() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbNhanVien = new javax.swing.JTable();
        jtfMaNhanVien = new javax.swing.JTextField();
        jbtCapNhat = new javax.swing.JButton();
        jtbThoat = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jpfMatKhau = new javax.swing.JPasswordField();
        jpfXacNhanMatKhau = new javax.swing.JPasswordField();
        jLabel10 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Quản Lý Mật Khẩu Nhân Viên");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jLabel1.setText("Mã Nhân Viên:");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Nhân Viên"));

        jtbNhanVien.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtbNhanVien.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbNhanVienMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtbNhanVien);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
        );

        jtfMaNhanVien.setEditable(false);

        jbtCapNhat.setText("Cập Nhật");
        jbtCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtCapNhatActionPerformed(evt);
            }
        });

        jtbThoat.setText("Thoát");
        jtbThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtbThoatActionPerformed(evt);
            }
        });

        jLabel9.setText("Mật Khẩu:");

        jLabel10.setText("Xác Nhận Mật Khẩu:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jtfMaNhanVien, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                            .addComponent(jpfMatKhau)
                            .addComponent(jpfXacNhanMatKhau))
                        .addGap(0, 379, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jbtCapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtbThoat)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfMaNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jpfMatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jpfXacNhanMatKhau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtCapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtbThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    

public void hienThiNhanVien(){
   
        jtbNhanVien.removeAll();
        dsNhanVien = nhanvienDAO.layDanhSachNhanVienTonTai();
        //Tạo model cho Table
        DefaultTableModel dtm = new DefaultTableModel();
        dtm.addColumn("Mã");
        dtm.addColumn("Tên Nhân Viên");
        dtm.addColumn("Bộ Phận");
        dtm.addColumn("Địa Chỉ");
        dtm.addColumn("Điện Thoại");
        dtm.addColumn("Email");
        dtm.addColumn("Số Tài Khoản");
        dtm.addColumn("Ngân Hàng");
        
        dtm.setNumRows(dsNhanVien.size());
        for(int i = 0; i<dsNhanVien.size(); i++){
            Nhanvien nv =  dsNhanVien.get(i);
            dtm.setValueAt(nv.getManhanvien(), i, 0);
            dtm.setValueAt(nv.getTennhanvien(), i, 1);
            dtm.setValueAt(nv.getBophan().getTenbophan(), i, 2);
            dtm.setValueAt(nv.getDiachi(), i, 3);
            dtm.setValueAt(nv.getDienthoai(), i, 4);
            dtm.setValueAt(nv.getEmail(), i, 5);
            dtm.setValueAt(nv.getSotaikhoan(), i, 6);
            dtm.setValueAt(nv.getNganhang(), i, 7);
        }
        //Hiển thị lên Table
        this.jtbNhanVien.setModel(dtm);
        this.jtbNhanVien.changeSelection(0, 0, false, false);
    
    }
    private void jbtCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtCapNhatActionPerformed
        // TODO add your handling code here:
        Nhanvien nv = nhanvienDAO.layThongTinNhanVien(jtfMaNhanVien.getText().trim());
        
        if(nv!=null){
            /*
            nv.setTennhanvien(jtfTenNhanVien.getText());
            Bophan bp  =  (Bophan) jcbBoPhan.getItemAt(jcbBoPhan.getSelectedIndex());
            nv.setBophan(bp);
           
            nv.setDiachi(jtfDiaChi.getText());
            nv.setDienthoai(jtfDienThoai.getText());
            nv.setEmail(jtfEmail.getText());
            nv.setSotaikhoan(jtfSoTaiKhoan.getText());
            nv.setNganhang(jtfNganHang.getText());
            * 
            */
            //cap nhat mat khau
            String mk = new String(jpfMatKhau.getPassword());
            String xnmk = new String(jpfXacNhanMatKhau.getPassword());
            if(mk.equals(xnmk) == true){
                nv.setMatkhau(mk);
            }else{
                JOptionPane.showMessageDialog(this, "Mật khẩu xác nhận không đúng.", "Thông Báo", JOptionPane.WARNING_MESSAGE);
                jpfMatKhau.setText("");
                jpfXacNhanMatKhau.setText("");
                return;
            }
                
            boolean kq = nhanvienDAO.capNhatThongTinNhanVien(nv);
            if (kq == true) {
                hienThiNhanVien();
                JOptionPane.showMessageDialog(this, "Cập Nhật mật khẩu nhân viên thành công");
                jpfMatKhau.setText("");
                jpfXacNhanMatKhau.setText("");
            } else {
                JOptionPane.showMessageDialog(this, "Cập Nhật mật khẩu nhân viên thất bại");
            }
        } 
    }//GEN-LAST:event_jbtCapNhatActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
       
        hienThiNhanVien();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jtbThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtbThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jtbThoatActionPerformed

    private void jtbNhanVienMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbNhanVienMouseClicked
        // TODO add your handling code here:
        int dong = this.jtbNhanVien.getSelectedRow();
        Nhanvien nv = dsNhanVien.get(dong);
        
        this.jtfMaNhanVien.setText(String.valueOf(nv.getManhanvien()));
       
    }//GEN-LAST:event_jtbNhanVienMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtCapNhat;
    private javax.swing.JPasswordField jpfMatKhau;
    private javax.swing.JPasswordField jpfXacNhanMatKhau;
    private javax.swing.JTable jtbNhanVien;
    private javax.swing.JButton jtbThoat;
    private javax.swing.JTextField jtfMaNhanVien;
    // End of variables declaration//GEN-END:variables
}
